<template>
	<view class="container">
		<view class="lesson">
			<uni-forms ref="form" :model="form" labelWidth="80px">
				<!-- <uni-forms-item label="学号" name="studentCode">
					<uni-combox :candidates="studentCodeRange" placeholder="请输入学号" v-model="form.studentCode" @input="onStudentCode"></uni-combox>
				</uni-forms-item> -->
				<uni-forms-item label="学员" name="studentName">
					<uni-combox :candidates="studentNameRange" placeholder="请输入学号或学员姓名" v-model="form.studentName" @input="onStudentName"></uni-combox>
				</uni-forms-item>
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput v-model="form.remark" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit"
				style="color:#ffffff;backgroundColor:#993399;borderColor:#993399;">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		listStudent,
		addStudent
	} from "@/api/puyu/lesson";
	import { listNotExpiredStudents } from "@/api/puyu/student";

	export default {
		data() {
			return {
				// 学员数据
				studentList: [],
				lesson: {},
				form: {
					studentCode: "",
					studentName: "",
					remark: ""
				},
				rules: {
					studentCode: {
						rules: [{
							required: true,
							errorMessage: '学号不能为空'
						}]
					},
					studentName: {
						rules: [{
							required: true,
							errorMessage: '学员姓名不能为空'
						}]
					},
				}
			}
		},
		computed: {
			studentCodeRange() {
				if (!this.studentList) {
					return [];
				}
				let arr = [];
				this.studentList.map(item => {
					arr.push(item.studentCode);
				});
				return arr;
			},
			studentNameRange() {
				if (!this.studentList) {
					return [];
				}
				let arr = [];
				this.studentList.map(item => {
					arr.push(item.studentCode + '-' + item.studentName);
				});
				return arr;
			},
		},

		created() {
			// 获取学员下拉列表
			this.getStudentLsit();
		},
		onLoad(option) {
		  const item = JSON.parse(decodeURIComponent(option.item));
		  this.lesson = item;
		  this.lesson.classesId = item.classesId + '';
		  this.lesson.stadiumId = item.stadiumId + '';
		},

		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/** 查询学员列表 */
			getStudentLsit() {
				listNotExpiredStudents().then(response => {
					this.studentList = response.data;
				});
			},
			submit(ref) {
				this.loading = true;
				if (!this.form.studentCode) {
					this.$modal.msgError("学号不能为空!")
					return
				}
				this.form.lessonId = this.lesson.lessonId;
				this.form.coachId = this.lesson.coachId;
				this.$refs.form.validate().then(res => {
					addStudent(this.form).then(response => {
						this.$modal.msgSuccess("添加成功!")
						this.loading = false;
						this.$tab.navigateBack();
					})
				})
			},
			onStudentCode(e) {
				if (!this.studentList) {
					return;
				}
				let student = this.studentList.filter(item => item.studentCode == e)[0];
				if (student) {
					this.form.studentId = student.studentId;
					this.form.studentName = student.studentName;
					this.form.userId = student.userId;
					this.form.deptId = student.deptId;
				}
			},
			onStudentName(e) {
				if (!this.studentList) {
					return;
				}
				let studentCode = e.split("-")[0]
				let student = this.studentList.filter(item => item.studentCode == studentCode)[0];
				if (student) {
					this.form.studentId = student.studentId;
					this.form.studentCode = student.studentCode;
					this.form.studentName = student.studentName;
					this.form.userId = student.userId;
					this.form.deptId = student.deptId;
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.lesson {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>